package portal.monitor.cmd;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import beans.HttpConfig;
import beans.HttpResult;
import net.CommitJob;
import portal.common.MesageCode;
import portal.common.ResultData;
import portal.monitor.utils.MonBaseUtils;
import portal.monitor.utils.MonConfig;
import portal.monitor.utils.ScheUtils;
import utils.Log4jInit;

public class ScheCmd {
	
	
	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
	
	public static ResultData startSche(String ips,String modeType){

		ResultData rd = new ResultData();
		HttpResult tokenResult = ScheUtils.getToken(HttpConfig.userName, HttpConfig.passWord);
		if(!tokenResult.isResult()){
			rd.resultCode = MesageCode.SUCCESS;
			rd.desc = "获取token失败";
			return rd;
		}
		String token = tokenResult.getMessage();
	
		
		String cmd = "net start "+MonConfig.SPexe+" & net stop "+MonConfig.SPexe;
		String taskHash = "START-"+modeType+"-"+df.format(new Date());
		
		String sendRusult = sendModeTxt(modeType,"start",ips);
		if(!sendRusult.equals("")){
			rd.resultCode = MesageCode.ERROR;
			rd.data = "启动TXT传输失败 : "+sendRusult;
			return rd;
		}
		
		//cmd script
		String scriptUri = MonConfig.localBase  + "run.bat";
		File algorithmBatFile = new File(scriptUri);
	//	if(!algorithmBatFile.exists()){
			try {
				
				FileWriter fw = new FileWriter(algorithmBatFile);
				MonBaseUtils.chmod(scriptUri);
				fw.write(cmd);
				fw.flush();
				fw.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	//	}
		
		
		CommitJob commitJob = new CommitJob(token, "run.bat",taskHash, "1", HttpConfig.userName,
				"windows","50",HttpConfig.gpuType, false,null,ips, false,null,"-ut",scriptUri,null);
		HttpResult startResult = ScheUtils.startJob(commitJob);
		
		if(startResult.isResult()){
//			MonBaseUtils.createModeTag(modeType, ips);
			rd.resultCode = MesageCode.SUCCESS;
			rd.data = startResult.getMessage();
		}else{
			rd.resultCode = MesageCode.ERROR;
			rd.data = "作业发起失败.";
		}
		rd.resultCode = MesageCode.SUCCESS;
		rd.data = "OK";
		return rd;
	}
	
	
	public static ResultData stopProc(String IP,String KLPath){

		ResultData rd = new ResultData();
	Log4jInit.logger.info("-------KILL PROC IP:"+IP);
			HttpResult tokenResult = ScheUtils.getToken(HttpConfig.userName, HttpConfig.passWord);
			if(!tokenResult.isResult()){
				
				rd.resultCode = MesageCode.SUCCESS;
				rd.desc = "获取token失败";
				return rd;
			}
			String token = tokenResult.getMessage();
			
			String exePath = MonConfig.localBase + "deploy/"+ MonConfig.GISexe;
			String KPPath = MonConfig.localBase + "kill/KP.txt";
			String fileUpload ="";
			if(MonConfig.useCopy){
				boolean b =MonBaseUtils.copyFile(KLPath, KPPath);
				if(b==false){
					rd.resultCode=MesageCode.ERROR;
					rd.data = "KP本地拷贝错误。";
					return rd;
				}
				fileUpload =KPPath +" "+ exePath;
				
			}else{
				List<String> list = new ArrayList<String>();
				String execPath = ScheCmd.class.getResource("/"+MonConfig.execclientUri).getPath();
				try {
					MonBaseUtils.updateState(exePath, MonBaseUtils.generateScriptFile(exePath, exePath, execPath), list);
					MonBaseUtils.updateState(KPPath, MonBaseUtils.generateScriptFile(KPPath, KPPath, execPath), list);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for(String s:list){
					fileUpload = fileUpload+" "+s;
				}
			}
		
			String cmd = "GIS.exe 3 ";
			String taskHash = "KILL-PROCS-"+df.format(new Date());
			
			CommitJob commitJob = new CommitJob(token, cmd,taskHash, "1", HttpConfig.userName,
					"windows","50",HttpConfig.gpuType, false,null,IP , false,null,"-ut",fileUpload,null);
			HttpResult startResult = ScheUtils.startJob(commitJob);
			
			if(startResult.isResult()){
				rd.resultCode = MesageCode.SUCCESS;
				rd.data = startResult.getMessage();
			}else{
				rd.resultCode = MesageCode.ERROR;
				rd.data = "作业发起失败.";
			}
			rd.resultCode = MesageCode.SUCCESS;
			rd.data = "OK";
			return rd;
			
	}
	
	
	public static ResultData stopSche(String IPS,String modeType){
		
		ResultData rd = new ResultData();
	
			HttpResult tokenResult = ScheUtils.getToken(HttpConfig.userName, HttpConfig.passWord);
			if(!tokenResult.isResult()){
				
				rd.resultCode = MesageCode.SUCCESS;
				rd.desc = "获取token失败";
				return rd;
			}
			String token = tokenResult.getMessage();
			
			String exePath = MonConfig.localBase + "deploy/"+ MonConfig.GISexe;
			String KPPath = MonConfig.localBase + "kill/KP.txt";
			String fileUpload ="";
			if(MonConfig.useCopy){
				boolean b =MonBaseUtils.copyFile(MonConfig.localBase + "kill/"+modeType+".txt", KPPath);
				if(b==false){
					rd.resultCode=MesageCode.ERROR;
					rd.data = "KP本地拷贝错误。";
					return rd;
				}
				fileUpload =KPPath +" "+ exePath;
				
			}else{
				List<String> list = new ArrayList<String>();
				String execPath = ScheCmd.class.getResource("/"+MonConfig.execclientUri).getPath();
				try {
					MonBaseUtils.updateState(exePath, MonBaseUtils.generateScriptFile(exePath, exePath, execPath), list);
					MonBaseUtils.updateState(KPPath, MonBaseUtils.generateScriptFile(KPPath, KPPath, execPath), list);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for(String s:list){
					fileUpload = fileUpload+" "+s;
				}
			}
		
			String cmd = "GIS.exe 3 ";
			String taskHash = "KILL-"+modeType+"-"+df.format(new Date());
			
			CommitJob commitJob = new CommitJob(token, cmd,taskHash, "1", HttpConfig.userName,
					"windows","50",HttpConfig.gpuType, false,null,IPS, false,null,"-ut",fileUpload,null);
			HttpResult startResult = ScheUtils.startJob(commitJob);
			
			if(startResult.isResult()){
				rd.resultCode = MesageCode.SUCCESS;
				rd.data = startResult.getMessage();
			}else{
				rd.resultCode = MesageCode.ERROR;
				rd.data = "作业发起失败.";
			}
			rd.resultCode = MesageCode.SUCCESS;
			rd.data = "OK";
			return rd;
	}
	
	private static String sendModeTxt(String modeName,String type,String IPS){
		
		String result = "";
		String sendTxt = MonConfig.localBase+type+"/"+modeName+".txt";
		String windowsTxt = "";
		windowsTxt = type.equals("kill")?"KP":"SP";
		String sendCmd = MonConfig.remoteExecPath+MonConfig.fileclient + " -m " + MonConfig.localhost + " -l " + MonConfig.remoteTxtPath +windowsTxt+".txt" + " -r " +sendTxt + " -d -p 6522 -n jhadmin";
		String[] ips = IPS.split(",");
		for(String s :ips){
			String tmp = ExcuCmd.execclient3(sendCmd, s);
			if(!tmp.contains("success")){
				result = result+s+":ERROR  ";
				break;
			}
		}
		return result;
	}
	
	public static ResultData excuteSched(String cmd,String IPS,String uploadFiles,String taskHashSuff){
		
		ResultData rd = new ResultData();
		
		HttpResult tokenResult = ScheUtils.getToken(HttpConfig.userName, HttpConfig.passWord);
		if(!tokenResult.isResult()){
			
			rd.resultCode = MesageCode.SUCCESS;
			rd.desc = "获取token失败";
			return rd;
		}
		String token = tokenResult.getMessage();
		
	
		String taskHash = taskHashSuff+"-"+df.format(new Date());
		
		CommitJob commitJob = new CommitJob(token, cmd,taskHash, "1", HttpConfig.userName,
				"windows","50",HttpConfig.gpuType, false,null,IPS, false,"","-ut",uploadFiles,null);
		HttpResult startResult = ScheUtils.startJob(commitJob);
		
		if(startResult.isResult()){
			rd.resultCode = MesageCode.SUCCESS;
			rd.data = startResult.getMessage();
		}else{
			rd.resultCode = MesageCode.ERROR;
			rd.data = "作业发起失败.";
		}
		rd.resultCode = MesageCode.SUCCESS;
		rd.data = "OK";
		return rd;
		
	}

	
	
}
